﻿using Domain.IRepositorys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class SystemHomeRepository : ISystemHomeRepository
    {
        private readonly MyDbContext myDbContext;

        public SystemHomeRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        /// <summary>
        /// 查询业务单总数
        /// </summary>
        /// <returns></returns>
        public int GetBusinessCounts()
        {
            //return myDbContext.Sinv_Sale_Order.Where(s => s.Del_flag == '0' && s.Sale_order_audit_status == '1').Count()
            //    + myDbContext.Sinv_Sale_Outbound.Where(s => s.Del_flag == '0' && s.Sale_outbound_audit_status == '1').Count()
            //    + myDbContext.Sinv_Sale_Return.Where(s => s.Del_Flag == '0' && s.Sale_Return_Audit_Status == '1').Count()
            //    + myDbContext.Pinv_Po.Where(s => s.Del_Flag == '0' && s.Po_Audit == '1').Count()
            //    + myDbContext.pinv_Pb.Where(s => s.Del_flag == '0' && s.Pb_Audit == '1').Count()
            //    + myDbContext.Pinv_purret.Where(s => s.Del_flag == '0' && s.Pu_audit == '1').Count()
            //    + myDbContext.FG_Finished_Goods_Store.Where(s => s.Del_flag == '0' && s.Finished_goods_store_audit_status == '1').Count()
            //    + myDbContext.FG_Picking_Outbound.Where(s => s.Del_flag == '0' && s.Picking_outbound_audit_status == '1').Count()
            //    + myDbContext.inv_trf.Where(s => s.Del_flag == '0' && s.trf_audit_status == "1").Count()
            //    + myDbContext.inv_othwr.Where(s => s.del_flag == '0' && s.othwr_audit_statua == "1").Count()
            //    + myDbContext.inv_othoutord.Where(s => s.Del_flag == '0' && s.Othoutord_audit_statua == "1").Count()
            //    + myDbContext.inv_samp.Where(s => s.Del_flag == '0' && s.Samp_audit_status == '1').Count()
            //    + myDbContext.inv_qty.Where(s => s.Del_flag == '0' && s.qty_audit_status == '1').Count();
            return myDbContext.Sinv_Sale_Order.Where(s => s.Del_flag == '0' && s.Sale_order_audit_status == '1').Count()
                + myDbContext.Sinv_Sale_Outbound.Where(s => s.Del_flag == '0' && s.Sale_outbound_audit_status == '1').Count()
                + myDbContext.Sinv_Sale_Return.Where(s => s.Del_Flag == '0' && s.Sale_Return_Audit_Status == '1').Count();
        }

        /// <summary>
        /// 查询出库总数
        /// </summary>
        /// <returns></returns>
        public decimal GetOutboundCounts()
        {
            //销售订单出库总数
            decimal salesSum = myDbContext.Sinv_Sale_Order_Details
                .Join(myDbContext.Sinv_Sale_Order,
                d => d.Sale_order_id,
                s => s.Sale_order_id,
                (d, s) => new { d, s })
                .Where(x => x.s.Del_flag == '0' && x.s.Sale_order_audit_status == '1')
                .Sum(x => x.d.Pro_outbound_qty);
            //采购订单出库总数
            decimal poSum = myDbContext.pinv_details
                .Join(myDbContext.Pinv_Po,
                d => d.Po_Id,
                p => p.Po_Id,
                (d, p) => new { d, p })
                .Where(x => x.p.Del_Flag == '0' && x.p.Po_Audit == '1')
                .Sum(x => x.d.Podet_retqty);
            //其他出库总数
            decimal othprodSum = myDbContext.inv_othproddetails
                .Join(myDbContext.inv_othoutord,
                d => d.Othoutord_id,
                o => o.Othoutord_id,
                (d, o) => new { d, o })
                .Where(x => x.o.Del_flag == '0' && x.o.Othoutord_audit_statua == "1")
                .Sum(x => Convert.ToDecimal(x.d.Othwrproddt_numder));
            return salesSum + poSum ;
        }

        /// <summary>
        /// 查询退货总数
        /// </summary>
        /// <returns></returns>
        public decimal GetReturnCounts()
        {
            //销售订单退货总数
            decimal salesSum = myDbContext.Sinv_Sale_Order_Details
                .Join(myDbContext.Sinv_Sale_Order,
                d => d.Sale_order_id,
                s => s.Sale_order_id,
                (d, s) => new { d, s })
                .Where(x => x.s.Del_flag == '0' && x.s.Sale_order_audit_status == '1')
                .Sum(x => x.d.Pro_return_qty);
            //采购订单退货总数
            decimal poSum = myDbContext.pinv_details
                .Join(myDbContext.Pinv_Po,
                d => d.Po_Id,
                p => p.Po_Id,
                (d, p) => new { d, p })
                .Where(x => x.p.Del_Flag == '0' && x.p.Po_Audit == '1')
                .Sum(x => x.d.Podet_retqty);
            //return myDbContext.Sinv_Sale_Order_Details.Sum(s => s.Pro_return_qty)
            //    + myDbContext.pinv_details.Sum(p => p.Podet_retqty);
            return salesSum;
        }

        /// <summary>
        /// 查询入库总数
        /// </summary>
        /// <returns></returns>
        public decimal GetStoreCounts()
        {
            //销售订单入库总数
            decimal salesSum = myDbContext.Sinv_Sale_Order_Details
                .Join(myDbContext.Sinv_Sale_Order,
                d => d.Sale_order_id,
                s => s.Sale_order_id,
                (d, s) => new { d, s })
                .Where(x => x.s.Del_flag == '0' && x.s.Sale_order_audit_status == '1')
                .Sum(x => x.d.Pro_return_qty);
            //采购订单入库总数
            decimal poSum = myDbContext.pinv_details
                .Join(myDbContext.Pinv_Po,
                d => d.Po_Id,
                p => p.Po_Id,
                (d, p) => new { d, p })
                .Where(x => x.p.Del_Flag == '0' && x.p.Po_Audit == '1')
                .Sum(x => x.d.Details_storage);
            //其他入库总数
            decimal othwrSum = myDbContext.inv_othwrproddtl
                .Join(myDbContext.inv_othwr,
                o => o.Othwr_id,
                d => d.Othwr_id,
                (d, o) => new { d, o })
                .Where(x => x.o.del_flag == '0' && x.o.othwr_audit_statua == "1")
                .Sum(x => x.d.Othwrproddtl_numder);
            //return myDbContext.Sinv_Sale_Order_Details.Sum(s => s.Pro_return_qty)
            //    + myDbContext.pinv_details.Sum(p => p.Details_storage)
            //    + myDbContext.inv_othwrproddtl.Sum(p => p.Othwrproddtl_numder);
            return salesSum + poSum + othwrSum;
        }
    }
}
